//获取应用实例
var app = getApp()
var Crypto = require('../../utils/cryptojs/cryptojs.js').Crypto;
var RdWXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js')
var util = require('../../utils/util.js')
var appId = 'wx96d8ff8965fb2883'
var sessionKey = 'tiihtNczf5v6AKRyjwEUhQ=='
var appSecret ='b357b6e77917e0fc8c49cb9c22e40fc6'
// var pc = new WXBizDataCrypt(appId, sessionKey)
// var data = pc.decryptData(encryptedData, iv)
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: "../../resources/Jellyfish.jpg"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  var that=this;
    wx.getStorage({
      key: app.globalData.storage_userinfo_key,
      success: function (res) {
        if (res.data!= null) {
          wx.login({
            success: function (res) {
              wx.request({
                url: 'https://api.weixin.qq.com/sns/jscode2session',
                data: {
                  appid: appId,
                  secret: appSecret,
                  js_code: res.code,
                  grant_type: 'authorization_code'
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'GET',
                success: function (res) {
                  var pc = new RdWXBizDataCrypt(appId, res.data.session_key)
                  wx.getUserInfo({
                    success: function (res) {
                      var data = pc.decryptData(res.encryptedData, res.iv)
                      util.netWorkUtil('user/wx/login', {
                        userNickName: data.nickName,
                        userHeadUrl: data.avatarUrl,
                        userWXunionId: data.unionId,
                      }, function (err, res) {
                        app.globalData.userInfo=res.data.data,
                        console.log('onLoad netWork: ', res.data.data)
                        //保存用户信息到本地
                        wx.setStorage({
                          key: app.globalData.storage_userinfo_key,
                          data: res.data.data,
                        })
                      });
                      console.log('onLoad data: ', data)
                      that.toHome();
                      
                    }
                  })
                },
                fail: function (res) { },
                complete: function (res) { }
              })
            },
            fail: function (res) {

            }
          })
        } else {
          that.authDialog = that.selectComponent("#authDialog");
          that.authDialog.showDialog();
        }
      },
      fail: function (res) {
        that.authDialog = that.selectComponent("#authDialog");
        that.authDialog.showDialog();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 跳转到首页
   */
  toHome:function(){
    setTimeout(function () {
      wx.switchTab({
        url: '../../pages/filmreview/filmreview',
      })
    }, 3000)
  },

  cancelEvent: function () {
   this.toHome();
    this.authDialog.hideDialog();
  },
  confirmEvent: function () {
    this.authDialog.hideDialog();
  },


  bindGetUserInfo: function (e) {
    var that = this
    // 用户点击授权后，这里可以做一些登陆操作
    console.log('bindGetUserInfo: ', e.detail)
    if (e.detail) {
      getApp().globalData.userInfo = e.detail;
      wx.login({
        success: function (res) {
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
              appid: appId,
              secret: appSecret,
              js_code: res.code,
              grant_type: 'authorization_code'
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'GET', 
            success: function (res) {
              var pc = new RdWXBizDataCrypt(appId, res.data.session_key)
              wx.getUserInfo({
                success: function (res) {
                  var data = pc.decryptData(res.encryptedData, res.iv)
                  util.netWorkUtil('user/wx/login', {
                    userNickName: data.nickName,
                    userHeadUrl: data.avatarUrl,
                    userWXunionId: data.unionId,
                  }, function (err, res) {
                    app.globalData.userInfo = res.data.data,
                    console.log('bindGetUserInfo netWork: ', res)
                    wx.setStorage({
                      key: app.globalData.storage_userinfo_key,
                      data: res.data.data,
                    })
                  });
                  console.log('bindGetUserInfo解密后 data: ', data)
                  that.toHome();
                  //保存用户信息到本地
                 
                }
              })
            },
            fail: function (res) { },
            complete: function (res) { }
          })
        },
        fail: function (res) {

        }
      })
    } else {
      wx.showModal({
        title: '登录失败',
        content: '请重新登录，以享受更多功能',
        showCancel: false, //不显示取消按钮
        confirmText: '确定'
      })
    }
  },
})
